//
//  ZYRequestCore.swift
//  SwiftDemo1
//
//  Created by wangzhipan on 2025/6/13.
//

import Foundation


class ZYRequestCore {
    static let shareCore = ZYRequestCore()
    
    func loadAccessToken(code: String, completed: @escaping (_ result: ZYHttpResult<ZYUserAccount>) -> Void) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id" : app_key, "client_secret" : app_secret, "grant_type" : "authorization_code", "redirect_uri" : redirect_uri, "code" : code]
        ZYHttpClient.shareInstance.post(urlString, parameters: parameters ,responseType: ZYUserAccount.self) { result in
            completed(result)
        }
    }
    
    func loadUserInfo(account: ZYUserAccount, completed: @escaping (_ result: ZYHttpResult<ZYUserTempInfo>) -> Void) {
        
        guard let accessToken = account.access_token, let uid = account.uid else {
            return
        }
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parameters = ["access_token" : accessToken, "uid" : uid]
        ZYHttpClient.shareInstance.get(urlString, parameters: parameters ,responseType: ZYUserTempInfo.self) { result in
            completed(result)
        }
    }
}

// MARK: -首页请求
extension ZYRequestCore {
    func loadHomeStatuses(since_id: Int, max_id: Int, completed: @escaping (_ result: ZYHttpResult<[String: Any]>) -> Void) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        guard let accessToken = ZYUserCore.shareCore.account?.access_token else {
            Logger.info("request error: access_token is nil")
            return
        }
        let parameters = ["access_token" : accessToken, "since_id" : "\(since_id)", "max_id" : "\(max_id)"]
        ZYHttpClient.shareInstance.getDictionary(urlString, parameters: parameters) { result in
            completed(result)
        }
        /*
         https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00mOGawCuT39gD713c4f75d0vxUs9E&since_id=0&max_id=0
         */
    }
}
